// packageStore/pages/profile/subMenu/index.js
import {util} from '../../../../js/util';
import {password}  from "../../../../js/modules/account.js";
import {router} from '../../../../js/router';
import BasePage from '../../../../js/common/base-page'
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    PageIndex:1,
    PageSize:10,
    hasMoreData:true,
    menuList:[],
    Keyword:"",
    isLoadComplete: false, // 是否已加载数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMenuList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      var index = this.data.PageIndex;
      this.setData({
        PageIndex:index+1
      },function(){
        this.getMenuList();
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 子账号列表
  getMenuList(){
    let that = this;
    let data ={PageIndex:that.data.PageIndex,PageSize:that.data.PageSize,keywords: that.data.Keyword,}
    password.managerList(data,function(res){
      var data = res.Data;
      let hasMore = res.TotalCount > that.data.PageIndex * that.data.PageSize;
      //当分页大于1时，累加
      if (that.data.PageIndex > 1) {
        data = that.data.menuList.concat(data);
      }
      that.setData({
        menuList:data,
        hasMoreData: hasMore,
        isLoadComplete: true
      })
      // console.log(res,'=====1');
    })
  },
  switch1Change(e){
    var item = e.currentTarget.dataset.item;
    // console.log(item,'==========1',item.Name);
    let data ={
      Id:item.Id,
      Status:e.detail.value,
      UserName:item.UserName,
      Password:item.Password,
      Name:item.Name,
      NickName:item.NickName
    }
    password.managerUpdate(data,function(res){
      util.tips('操作成功');
    })
  },
  //添加子账号
  addSubAccount(e){
    router.navigate({
      path: "addSubAccount"
    });
  },
  //修改子账号
  bindEdit(e){
    var item = e.currentTarget.dataset.item;
    router.navigate({
      path: "addSubAccount",
      params: {
        item:JSON.stringify(item),
        type:'edit'
      }
    });
  },
  onSearchEvent(e){
    var keyword=e.detail;
    this.setData({
      Keyword: keyword,
      PageIndex:1,
      isLoadComplete: false
    });
    this.getMenuList();
  }
})